package com.cfets.annualAssessment.util;

import com.cfets.annualAssessment.entity.AnnualInform;
import com.qcloud.sms.SmsStatusPullReplyResult;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
/**
 * Created by ShenYiChou on 2017/3/2.
 */
public class TextUtil {

    public static void writeEmailToTxt(HttpServletResponse response, List<AnnualInform> List) {
        response.setContentType("text/plain");// 一下两行关键的设置
        response.addHeader("Content-Disposition",
                "attachment;filename=E_mail.txt");// filename指定默认的名字
        BufferedOutputStream buff = null;
        StringBuilder write = new StringBuilder();
        ServletOutputStream outSTr = null;
        try {
            outSTr = response.getOutputStream();// 建立
            buff = new BufferedOutputStream(outSTr);
            for (AnnualInform item:List) {
                write.append(item.getE_mail()).append(";");
            }
            buff.write(write.toString().getBytes("UTF-8"));
            buff.flush();
            buff.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (buff != null) {
                    buff.close();
                }
                if (outSTr != null) {
                    outSTr.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void SMSResultsToTxt(HttpServletResponse response, String ResTemp) throws Exception {
        response.setContentType("text/plain");// 一下两行关键的设置
        response.addHeader("Content-Disposition",
                "attachment;filename=SMSResults.txt");// filename指定默认的名字
        SmsStatusPullReplyResult reply_result = SmsSDKDemo.replyResult();
        BufferedOutputStream buff = null;
        StringBuilder write = new StringBuilder();
        ServletOutputStream outSTr = null;
        try {
            outSTr = response.getOutputStream();// 建立
            buff = new BufferedOutputStream(outSTr);
            for (String reply:reply_result.getReplays()) {
                String[] rep = reply.split(" ");
                String[] temp = ResTemp.split(" ");
                for (int i=0; i<rep.length; i++) {
                    write.append(temp[i]).append(": ");
                    write.append(rep[i]).append(", ");
                    if(i == (rep.length-1)){
                        write.append("\r\n");
                    }
                }
            }
            buff.write(write.toString().getBytes("UTF-8"));
            buff.flush();
            buff.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (buff != null) {
                    buff.close();
                }
                if (outSTr != null) {
                    outSTr.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}
